package io.coderyeah.org.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.coderyeah.basic.Result;
import io.coderyeah.org.dto.DeptDTO;
import io.coderyeah.org.domain.Department;
import io.coderyeah.org.mapper.DepartmentMapper;
import io.coderyeah.org.service.DepartmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


@Service
@Transactional
public class DepartmentServiceImpl extends ServiceImpl<DepartmentMapper, Department> implements DepartmentService {
    @Autowired
    private DepartmentMapper departmentMapper;

    @Override
    public Result list(DeptDTO deptDTO) {
        //分页 默认CurrentPage=1  PageSize=5
//        final IPage<Department> page = new Page<>(deptDTO.getCurrentPage(), deptDTO.getPageSize());
//        final LambdaQueryWrapper<Department> wrapper = getWrapper(deptDTO);
        //分页条件查询
//        final IPage<Department> selectPage = departmentMapper.selectPage(page, wrapper);
        //总条数
//        final long total = selectPage.getTotal();
//        final long size = selectPage.getSize();
        //结果集
//        final List<Department> list = selectPage.getRecords();
        // 查询部门列表
        List<Department> departments = departmentMapper.getList(deptDTO);
        // 查询数量
        final LambdaQueryWrapper<Department> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(Department::getSn, deptDTO.getKeyword()).or().like(Department::getName, deptDTO.getName());
        Integer total = departmentMapper.selectCount(wrapper);
        System.out.println("total:" + total);
        return Result.success(departments, Long.parseLong(total.toString()));
    }

    @Override
    public Result saveOrUpdate(DeptDTO deptDTO) {
        if (deptDTO.getId() != null) {
            //修改部门
            final Department department = BeanUtil.copyProperties(deptDTO, Department.class);
            departmentMapper.updateById(department);
            return Result.success("修改部门成功");
        } else {
            //添加部门
            final Department department = BeanUtil.copyProperties(deptDTO, Department.class);
            departmentMapper.insert(department);
            return Result.success("添加部门成功");
        }
    }

    //判断是否需要查询条件
    private LambdaQueryWrapper<Department> getWrapper(DeptDTO deptDTO) {
        final LambdaQueryWrapper<Department> wrapper = new LambdaQueryWrapper<>();
        if (StrUtil.isNotBlank(deptDTO.getName())) {
            wrapper.eq(Department::getName, deptDTO.getName());
        }
        if (StrUtil.isNotBlank(deptDTO.getSn())) {
            wrapper.eq(Department::getSn, deptDTO.getSn());
        }
        if (deptDTO.getState() != null) {
            wrapper.eq(Department::getState, deptDTO.getState());
        }
        if (deptDTO.getManagerId() != null) {
            wrapper.eq(Department::getManagerId, deptDTO.getManagerId());
        }
        if (deptDTO.getParentId() != null) {
            wrapper.eq(Department::getParentId, deptDTO.getParentId());
        }
        return wrapper;
    }

    @Override
    public Result editType(DeptDTO deptDTO) {
        departmentMapper.editType(deptDTO.getState() == 1 ? 0 : 1, deptDTO.getId());
        return Result.success("更新状态成功");
    }

    @Override
    public Result getDepartmentTrees() {
        final ArrayList<Department> list = new ArrayList<>();
        //查询所有部门数据
        final List<Department> departments = departmentMapper.selectList(new LambdaQueryWrapper<>());
        for (Department department : departments) {
            if (null != department.getParentId()) {
                //表示为子级部门 找到唯一父级部门
            /*   双重for循环实现无限级树效率慢 所以使用HashMap
             for (Department parent : departments) {
                    if (department.getParentId().equals(parent.getId())) {
                        parent.getChildren().add(department);
                        break;//找到一个父级部门就跳出循环相当于一个父亲
                    }
                }*/
                // 实现无限级树高级版^_^
                // 将集合转成map 键是id 值是部门类
                Map<Long, Department> map = departments.stream().collect(Collectors.toMap(Department::getId, e -> e));
                //上面一行代码相当于如下代码
             /*   final HashMap<Long, Department> map2 = new HashMap<>();
                for (Department d : departments) {
                    map2.put(d.getId(), d);
                }*/
                //获取父级部门
                final Department dept = map.get(department.getParentId());
                //将当前子级部门添加到父级部门
                dept.getChildren().add(department);
            } else {
                //顶级部门
                list.add(department);
            }
        }
        return Result.success(list);
    }
}
